package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    //保存所有文章信息的目录名字
    private static String userDirTitle = "./userTitles/";

    static {
        //程序加载时判断一下保存所有用户信息的目录是否存在，不存在先自动创建出来
        File uDT = new File(userDirTitle);
        if (!uDT.exists()) {
            uDT.mkdirs();
        }
    }

    public void writearticle(HttpRequest request, HttpResponse response) {
        System.out.println("处理发表文章。。。");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if (title == null || author == null || content == null) {
            response.setEntity(new File("./webapps/myweb/articleFail.html"));
            return;
        }
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(userDirTitle + title + ".obj")
                );
        ) {
            Article article = new Article(title, author, content);
            oos.writeObject(article);
            response.setEntity(new File("./webapps/myweb/articleSuccess.html"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("处理发表文章完毕！");
    }


    public void showAllArticle(HttpRequest request,HttpResponse response){
        System.out.println("开始生成文章信息列表。。。");
        List<Article> articleList = new ArrayList<>();
        File uDT = new File(userDirTitle);
        File[] subs = uDT.listFiles(f->f.getName().endsWith(".obj"));
        for (File file:subs){
            try(
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(file)
                    );
                    ){
                articleList.add((Article)ois.readObject());
            }catch (IOException | ClassNotFoundException e){
                e.printStackTrace();
            }
        }
        articleList.forEach(System.out::println);

        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">")                                ;
        pw.println("<title>文章信息列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章信息列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr align=\"center\">");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");

        for (Article article:articleList) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle()+"</td>");
            pw.println("<td>" + article.getAuthor()+"</td>");
            pw.println("<tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");

        System.out.println("文章信息列表生成完毕！");
    }

}
